const { DataTypes } = require('sequelize');
const { sequelizeTarget } = require('../../utils/db');
const {generateUUIDId} = require("../../utils/generatId");
const AuthInfo = require("./Auth");


const AmmeterInfo = sequelizeTarget.define('AmmeterInfo', {
    id: {
        type: DataTypes.STRING(255),
        primaryKey: true, // 设置为主键
        defaultValue: () => generateUUIDId(),
    },
    alias: {
        type: DataTypes.STRING(255),
        allowNull: false,
    },

    monitorId: {
        type: DataTypes.STRING(255),
        allowNull: true,
    },
    username: {
        type: DataTypes.STRING(255),
        allowNull: false,
        references: {
            model: AuthInfo,
            key: 'username',
        },
    },
}, {
    tableName: 'AmmeterInfo',
    timestamps: true,
});

// 同步模型到数据库
AmmeterInfo.sync()
    .then(() => {
        console.log('电表模型映射完成');
    })
    .catch((error) => {
        console.error('Error creating table:', error);
    });

module.exports = AmmeterInfo;
